﻿
using System;
using System.Collections.Generic;
using RpcClient.Interface;
using RpcClient.Model;
using RpcHelper;
using RpcModel;

namespace RpcClient
{
    internal class RpcService : IService
    {
        public static IService Service = new RpcService();
        public event ReceiveMsgEvent ReceiveMsg;



        public event LoadExtend LoadExtend;
        public event Action StartUpComplate;
        public event Action StartUpComplating;

        public event SendIng SendIng;
        public event ReceiveEndEvent ReceiveEnd;
        public event Action InitComplating;

        public event Action BeginIniting;

        public void SendEvent(IRemoteConfig config, IRemote remote, object page, int sendNum)
        {
            SendIng?.Invoke(config, remote, page, sendNum);
        }

        public Dictionary<string, string> LoadExtendEvent(string dictate)
        {
            if (LoadExtend == null)
            {
                return null;
            }
            Dictionary<string, string> extend = new Dictionary<string, string>();
            LoadExtend(dictate, ref extend);
            if (extend.Count == 0)
            {
                return null;
            }
            return extend;
        }
        public void StartUp()
        {
            if (StartUpComplate != null)
            {
                Delegate[] list = StartUpComplate.GetInvocationList();
                list.ForEachByParallel(a =>
                {
                    a.DynamicInvoke();
                });
            }
        }
        public void InitEvent()
        {
            if (this.InitComplating != null)
            {
                Delegate[] list = this.InitComplating.GetInvocationList();
                list.ForEachByParallel(a =>
                {
                    a.DynamicInvoke();
                });
            }
        }
        public void BeginInit()
        {
            if (this.BeginIniting != null)
            {
                Delegate[] list = this.BeginIniting.GetInvocationList();
                list.ForEachByParallel(a =>
                {
                    a.DynamicInvoke();
                });
            }
        }
        public void StartUpIng()
        {
            if (this.StartUpComplating != null)
            {
                Delegate[] list = this.StartUpComplating.GetInvocationList();
                list.ForEachByParallel(a =>
                {
                    a.DynamicInvoke();
                });
            }
        }
        public void ReceiveMsgEvent(IMsg msg)
        {
            ReceiveMsg?.Invoke(msg);
        }


        public void ReceiveEndEvent(RemoteMsg msg, TcpRemoteReply reply)
        {
            ReceiveEnd?.Invoke(msg, reply);
        }
    }
}
